package com.ldatum.crm.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ldatum.crm.dao.ContactsDao;
import com.ldatum.crm.dao.CustomerDao;
import com.ldatum.crm.daoImpl.ContactsDaoImpl;
import com.ldatum.crm.daoImpl.CustomerDaoImpl;
import com.ldatum.crm.domain.Contacts;
import com.ldatum.crm.domain.Customer;
import com.ldatum.crm.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {
	
	CustomerDao customerDao = new CustomerDaoImpl();
	ContactsDao contactsDao = new ContactsDaoImpl();
	
	@Override
	public void add(Customer customer) throws Exception {
		customer.setBuildDate(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()));
		customerDao.add(customer);
	}

	@Override
	public void delete(Customer customer, int id) throws Exception {
		customerDao.delete(customer,id);
	}

	@Override
	public void update(Customer customer, int id) throws Exception {
		customerDao.update(customer,id);
	}

	@Override
	public Customer findById(Class<Customer> clazz, int id) throws Exception {
		
		return customerDao.findById(clazz,id);
	}

	@Override
	public List<Customer> myCustomer(Class<Customer> clazz, String proprietor)
			throws Exception {
		List<Customer> list = customerDao.myCustomer(clazz,proprietor);
		for(Customer customer:list){
			Contacts contacts = contactsDao.findNameByNo(customer.getNo());
			customer.setLxr(contacts.getName());
			customer.setLxrId(contacts.getId());
		}
		return list;
	}

	@Override
	public String generateNo() throws Exception {
		String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
		return customerDao.generateNo(currentDate);
	}

	@Override
	public void deletes(Customer customer, String ids) throws Exception {
		ids = ids.substring(0,ids.length()-1);
		System.out.println(ids+":ids");
		customerDao.deletes(customer, ids);
	}
}
